package ltd.dujiabao.dualwrite.datasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

/**
 * @author Bowie Du
 * @date 2025/11/1 10:31
 */

public class DualWriteStatement implements Statement {
    private static final Logger logger = LoggerFactory.getLogger(DualWriteStatement.class);
    protected final Statement priorityStatement;
    protected final Statement secondaryStatement;
    protected final DualWriteConnection dualWriteConnection;

    public DualWriteStatement(Statement priorityStatement,
                              Statement secondaryStatement,
                              DualWriteConnection dualWriteConnection) {
        this.priorityStatement = priorityStatement;
        this.secondaryStatement = secondaryStatement;
        this.dualWriteConnection = dualWriteConnection;
    }

    @Override
    public ResultSet executeQuery(String sql) throws SQLException {
        ResultSet resultSet = priorityStatement.executeQuery(sql);
        try {
            secondaryStatement.executeQuery(sql);
        } catch (SQLException e) {
            logger.warn("secondaryStatement executeQuery fail.", e);
        }
        return resultSet;
    }

    @Override
    public int executeUpdate(String sql) throws SQLException {
        int effected = priorityStatement.executeUpdate(sql);
        try {
            secondaryStatement.executeUpdate(sql);
        } catch (SQLException e) {
            logger.warn("secondaryStatement executeUpdate fail.", e);
        }
        return effected;
    }

    @Override
    public void close() throws SQLException {
        priorityStatement.close();
        try {
            secondaryStatement.close();
        } catch (SQLException e) {
            logger.warn("secondaryStatement close fail.", e);
        }
    }

    @Override
    public int getMaxFieldSize() throws SQLException {
        return priorityStatement.getMaxFieldSize();
    }

    @Override
    public void setMaxFieldSize(int max) throws SQLException {
        priorityStatement.setMaxFieldSize(max);
        secondaryStatement.setMaxFieldSize(max);
    }

    @Override
    public int getMaxRows() throws SQLException {
        return priorityStatement.getMaxRows();
    }

    @Override
    public void setMaxRows(int max) throws SQLException {
        priorityStatement.setMaxRows(max);
        secondaryStatement.setMaxRows(max);
    }

    @Override
    public void setEscapeProcessing(boolean enable) throws SQLException {
        priorityStatement.setEscapeProcessing(enable);
        secondaryStatement.setEscapeProcessing(enable);
    }

    @Override
    public int getQueryTimeout() throws SQLException {
        return priorityStatement.getQueryTimeout();
    }

    @Override
    public void setQueryTimeout(int seconds) throws SQLException {
        priorityStatement.setQueryTimeout(seconds);
        secondaryStatement.setQueryTimeout(seconds);
    }

    @Override
    public void cancel() throws SQLException {
        priorityStatement.cancel();
        secondaryStatement.cancel();
    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
        return priorityStatement.getWarnings();
    }

    @Override
    public void clearWarnings() throws SQLException {
        priorityStatement.clearWarnings();
        secondaryStatement.clearWarnings();
    }

    @Override
    public void setCursorName(String name) throws SQLException {
        priorityStatement.setCursorName(name);
        secondaryStatement.setCursorName(name);
    }

    @Override
    public boolean execute(String sql) throws SQLException {
        boolean execute = priorityStatement.execute(sql);
        try {
            secondaryStatement.execute(sql);
        } catch (SQLException e) {
            logger.warn("secondaryStatement execute fail.", e);
        }
        return execute;
    }

    @Override
    public ResultSet getResultSet() throws SQLException {
        ResultSet resultSet = priorityStatement.getResultSet();
        try {
            secondaryStatement.getResultSet();
        } catch (SQLException e) {
            logger.warn("secondaryStatement getResultSet close fail.", e);
        }
        return resultSet;
    }

    @Override
    public int getUpdateCount() throws SQLException {
        return priorityStatement.getUpdateCount();
    }

    @Override
    public boolean getMoreResults() throws SQLException {
        return priorityStatement.getMoreResults();
    }

    @Override
    public void setFetchDirection(int direction) throws SQLException {
        priorityStatement.setFetchDirection(direction);
        secondaryStatement.setFetchDirection(direction);
    }

    @Override
    public int getFetchDirection() throws SQLException {
        return priorityStatement.getFetchDirection();
    }

    @Override
    public void setFetchSize(int rows) throws SQLException {
        priorityStatement.setFetchSize(rows);
        secondaryStatement.setFetchSize(rows);
    }

    @Override
    public int getFetchSize() throws SQLException {
        return priorityStatement.getFetchSize();
    }

    @Override
    public int getResultSetConcurrency() throws SQLException {
        return priorityStatement.getResultSetConcurrency();
    }

    @Override
    public int getResultSetType() throws SQLException {
        return priorityStatement.getResultSetType();
    }

    @Override
    public void addBatch(String sql) throws SQLException {
        priorityStatement.addBatch(sql);
        secondaryStatement.addBatch(sql);
    }

    @Override
    public void clearBatch() throws SQLException {
        priorityStatement.clearBatch();
        secondaryStatement.clearBatch();
    }

    @Override
    public int[] executeBatch() throws SQLException {
        int[] executeBatch = priorityStatement.executeBatch();
        try {
            secondaryStatement.executeBatch();
        } catch (SQLException e) {
            logger.warn("secondaryStatement executeBatch fail.", e);
        }
        return executeBatch;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return dualWriteConnection;
    }

    @Override
    public boolean getMoreResults(int current) throws SQLException {
        return priorityStatement.getMoreResults(current);
    }

    @Override
    public ResultSet getGeneratedKeys() throws SQLException {
        return priorityStatement.getGeneratedKeys();
    }

    @Override
    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        int effected = priorityStatement.executeUpdate(sql, autoGeneratedKeys);
        try {
            secondaryStatement.executeUpdate(sql, autoGeneratedKeys);
        } catch (SQLException e) {
            logger.warn("secondaryStatement executeUpdate fail.", e);
        }
        return effected;
    }

    @Override
    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        int effected = priorityStatement.executeUpdate(sql, columnIndexes);
        try {
            secondaryStatement.executeUpdate(sql, columnIndexes);
        } catch (SQLException e) {
            logger.warn("secondaryStatement executeUpdate fail.", e);
        }
        return effected;
    }

    @Override
    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        int effected = priorityStatement.executeUpdate(sql, columnNames);
        try {
            secondaryStatement.executeUpdate(sql, columnNames);
        } catch (SQLException e) {
            logger.warn("secondaryStatement executeUpdate fail.", e);
        }
        return effected;
    }

    @Override
    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        boolean execute = priorityStatement.execute(sql, autoGeneratedKeys);
        try {
            secondaryStatement.execute(sql, autoGeneratedKeys);
        } catch (SQLException e) {
            logger.warn("secondaryStatement execute fail.", e);
        }
        return execute;
    }

    @Override
    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        boolean execute = priorityStatement.execute(sql, columnIndexes);
        try {
            secondaryStatement.execute(sql, columnIndexes);
        } catch (SQLException e) {
            logger.warn("secondaryStatement execute fail.", e);
        }
        return execute;
    }

    @Override
    public boolean execute(String sql, String[] columnNames) throws SQLException {
        boolean execute = priorityStatement.execute(sql, columnNames);
        try {
            secondaryStatement.execute(sql, columnNames);
        } catch (SQLException e) {
            logger.warn("secondaryStatement execute fail.", e);
        }
        return execute;
    }

    @Override
    public int getResultSetHoldability() throws SQLException {
        return priorityStatement.getResultSetHoldability();
    }

    @Override
    public boolean isClosed() throws SQLException {
        return priorityStatement.isClosed();
    }

    @Override
    public void setPoolable(boolean poolable) throws SQLException {
        priorityStatement.setPoolable(poolable);
        secondaryStatement.setPoolable(poolable);
    }

    @Override
    public boolean isPoolable() throws SQLException {
        return priorityStatement.isPoolable();
    }

    @Override
    public void closeOnCompletion() throws SQLException {
        priorityStatement.closeOnCompletion();
        secondaryStatement.closeOnCompletion();
    }

    @Override
    public boolean isCloseOnCompletion() throws SQLException {
        return priorityStatement.isCloseOnCompletion();
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return priorityStatement.unwrap(iface);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return priorityStatement.isWrapperFor(iface);
    }
}
